﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop.Outlook;
using BusinessLogic;
using System.Xml;
using DataAccess;
using Entity;
using System.Windows.Forms;
using System.Diagnostics;
using Entity.RSS_Feed;

namespace Avatar
{
    partial class FormRegion1
    {
        #region Form Region Factory

        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Appointment)]
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Contact)]
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Task)]
        [Microsoft.Office.Tools.Outlook.FormRegionName("Avatar.FormRegion1")]
        public partial class FormRegion1Factory
        {
            // Occurs before the form region is initialized.
            // To prevent the form region from appearing, set e.Cancel to true.
            // Use e.OutlookItem to get a reference to the current Outlook item.
            private void FormRegion1Factory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
            {
            }
        }

        #endregion

        // Occurs before the form region is displayed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void FormRegion1_FormRegionShowing(object sender, System.EventArgs e)
        {
            this.credential = Globals.ThisAddIn.credential;
            if (this.credential == null) return;    ///用户未登录，不再尝试获取豆瓣信息。
                                                                  ///TODO:  可以考虑 获取用户公开信息
            //this.credential.LoginEmail = senderEmail;   // 发件人邮件 也就是登录用的email。

            this.credential.LoginEmail = this.SenderEmail;
            if(news == null)     //还没有取到 用户广播的相关信息
                news = AddinLogic.GetUserNews(this.Sender, credential);
            if(events == null)       //还没有取到 用户活动的相关信息
                events = AddinLogic.GetOneCityEvents(this.Sender, credential);

            string iconUrl = this.Sender.IconLink;
            if (!string.IsNullOrEmpty(iconUrl)) this.pictureBox1.ImageLocation = iconUrl;      //显示用户的豆瓣图片
            
            LoadUserNews();
            LoadOneCityEvent();
        }

        private void LoadOneCityEvent()
        {
            foreach (OneCityEvent item in events)
            {
                OneCityEventEditor editor = new OneCityEventEditor();
                editor.RefreshEditor(item);
                (winformOneCityEventHost.Child as OneCityEventHost).AddControl(editor);
            }
        }

        private void LoadUserNews()
        {
            foreach (UserNews item in news)
            {
                UserNewsEditor editor = new UserNewsEditor();
                editor.RefreshEditor(item);
                (winformUserBroadcastHost.Child as UserBroadcastHost).AddControl(editor);
            }
        }

        // Occurs when the form region is closed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void FormRegion1_FormRegionClosed(object sender, System.EventArgs e)
        { 
        }

        private UserCredential credential = null;   // addin startup 方法里获取load credential； 
        string senderEmail = null;
        private List<UserNews> news = null;
        private List<OneCityEvent> events = null;
        private People sender = null;
        public People Sender
        {
            get
            {
                if (sender == null)
                {
                    if (this.OutlookItem is MailItem)
                    {
                        MailItem mItem = this.OutlookItem as MailItem;
                        senderEmail = mItem.SenderEmailAddress;
                    }
                    else throw new NotSupportedException("Only Email item is supported in this version!");

                    sender = TranslateRSSLogic.GetPeopleFromEmail(senderEmail);
                }
                return sender;
            } 
        }

        private void goDouban_Click(object sender, EventArgs e)
        {
            Process.Start("iexplore.exe", @"http://www.douban.com/people/" + this.Sender.UID);
        }

        public string SenderEmail
        {
            get
            {
                if (this.OutlookItem is MailItem)
                {
                    MailItem mItem = this.OutlookItem as MailItem;
                    this.senderEmail = mItem.SenderEmailAddress;
                }
                else throw new NotSupportedException("Only Email item is supported in this version!");
                return senderEmail;
            }
            set
            {
                this.senderEmail = value;
            }
        }
    }
}
